package com.hd123.rumba.gwt.widget2.client.fielddef;

/**
 * 整数类型字段的定义
 * 
 * @author LiQi
 * 
 */
public class IntegerFieldDef extends FieldDef {

  /** 最小值，实际值应>=或>最小值（>=还是>由allowEqualsMin决定），为空则不限制 */
  protected Integer minValue;

  /** 是否允许等于最小值 */
  protected boolean allowEqualsMin = true;

  /** 最大值，实际值应<=或<最大值（<=还是<由allowEqualsMax决定），为空则不限制 */
  protected Integer maxValue;

  /** 是否允许等于最大值 */
  protected boolean allowEqualsMax = true;

  public IntegerFieldDef(String name, String caption) {
    super(name, caption, true);
  }

  public IntegerFieldDef(String name, String caption, boolean nullable, Integer minValue,
      boolean allowEqualsMin, Integer maxValue, boolean allowEqualsMax) {
    super(name, caption, nullable);
    this.minValue = minValue;
    this.allowEqualsMin = allowEqualsMin;
    this.maxValue = maxValue;
    this.allowEqualsMax = allowEqualsMax;
  }

  public FieldDef clone() {
    return new IntegerFieldDef(name, caption, nullable, minValue, allowEqualsMin, maxValue,
        allowEqualsMax);
  }

  public boolean isAllowEqualsMin() {
    return allowEqualsMin;
  }

  public void setAllowEqualsMin(boolean allowEqualsMin) {
    this.allowEqualsMin = allowEqualsMin;
  }

  public boolean isAllowEqualsMax() {
    return allowEqualsMax;
  }

  public void setAllowEqualsMax(boolean allowEqualsMax) {
    this.allowEqualsMax = allowEqualsMax;
  }

  public boolean validateText(String text, StringBuffer message) {
    return validateText(text, message, true);
  }

  public boolean validateText(String text, StringBuffer message, boolean checkNullable) {
    Integer value;
    if (text == null || text.trim().equals(""))
      value = null;
    else {
      try {
        value = new Integer(text);
      } catch (NumberFormatException e) {
        finalizeMessage(message, "文本不是合法的整数格式");
        return false;
      }
    }

    return validate(value, message, checkNullable);
  }

  public boolean validate(Integer value, StringBuffer message) {
    return validate(value, message, true);
  }

  /**
   * 验证传入的值是否合法
   * 
   * @param value
   * @return
   */
  public boolean validate(Integer value, StringBuffer message, boolean checkNullable) {
    boolean result = super.validate(value, message, checkNullable);
    if (value == null)
      return result;

    // minValue
    if (minValue != null
        && (value.compareTo(minValue) < 0 || (!allowEqualsMin && value.equals(minValue)))) {
      appendMessage(message, "应该大于" + (allowEqualsMin ? "等于" : "") + minValue);
      result = false;
    }

    // maxValue
    if (maxValue != null
        && (value.compareTo(maxValue) > 0 || (!allowEqualsMax && value.equals(maxValue)))) {
      appendMessage(message, "应该小于" + (allowEqualsMax ? "等于" : "") + maxValue);
      result = false;
    }

    finalizeMessage(message);
    return result;
  }

  public Integer getMinValue() {
    return minValue;
  }

  public void setMinValue(Integer minValue) {
    this.minValue = minValue;
  }

  public Integer getMaxValue() {
    return maxValue;
  }

  public void setMaxValue(Integer maxValue) {
    this.maxValue = maxValue;
  }
}
